package cn.renxuan.job;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;

import cn.renxuan.CommonUtil;
import cn.renxuan.service.domesticweather.IDomesticWeatherService;

@Component
public class WeatherXxlJob {

    private IDomesticWeatherService domesticWeatherService;

    @Autowired
    public WeatherXxlJob(IDomesticWeatherService domesticWeatherService) {
        this.domesticWeatherService = domesticWeatherService;
    }

    /**
     * 添加lundear天气数据
     */
    @XxlJob("addLundearWeatherJobHandler")
    public void addLundearWeatherJobHandler() throws Exception {
        CommonUtil.printThreadLog("addLundearWeatherJobHandler 开始执行...");
        String jobParam = XxlJobHelper.getJobParam();
        if (jobParam == null || jobParam.isEmpty()) {
            CommonUtil.printThreadLog("区域代码为空，任务结束");
            return;
        }

        try {
            List<String> areaCodes = Arrays.asList(jobParam.split("&"));
            List<CompletableFuture<Void>> futures = areaCodes.stream().map(areaCode -> {
                return CompletableFuture.runAsync(() -> {
                    try {
                        boolean result = domesticWeatherService.addLundearWeather(areaCode);
                        if (result) {
                            CommonUtil.printThreadLog("Lundear天气数据添加成功，区域代码: " + areaCode);
                        } else {
                            CommonUtil.printThreadLog("Lundear天气数据添加失败，区域代码: " + areaCode);
                        }
                    } catch (Exception ex) {
                        CommonUtil.printThreadLog("Lundear天气数据添加异常，区域代码: " + areaCode + ", 异常信息: " +
                                ex.getMessage());
                    }
                });
            }).collect(Collectors.toList());

            // 合并所有future并等待它们全部完成
            CompletableFuture<Void> allFutures = CompletableFuture.allOf(
                    futures.toArray(new CompletableFuture[0])).thenRun(() -> {
                        CommonUtil.printThreadLog("所有Lundear天气数据处理完成");
                    });

            // 等待所有异步全部执行完毕
            allFutures.join();

            // for (String areaCode : areaCodes) {
            // try {
            // boolean result = domesticWeatherService.addLundearWeather(areaCode);
            // if (result) {
            // CommonUtil.printThreadLog("Lundear天气数据添加成功，区域代码: " + areaCode);
            // } else {
            // CommonUtil.printThreadLog("Lundear天气数据添加失败，区域代码: " + areaCode);
            // }
            // } catch (Exception ex) {
            // CommonUtil.printThreadLog("Lundear天气数据添加异常，区域代码: " + areaCode + ", 异常信息: " +
            // ex.getMessage());
            // }
            // }
        } catch (Exception ex) {
            CommonUtil.printThreadLog("Lundear天气数据添加异常，Job参数: " + jobParam + ", 异常信息: " +
                    ex.getMessage());
        }
    }

    /**
     * 添加百度天气数据
     */
    @XxlJob("addBaiduWeatherJobHandler")
    public void addBaiduWeatherJobHandler() throws Exception {
        CommonUtil.printThreadLog("addBaiduWeatherJobHandler 开始执行...");
        String jobParam = XxlJobHelper.getJobParam();
        if (jobParam == null || jobParam.isEmpty()) {
            CommonUtil.printThreadLog("区域代码为空，任务结束");
            return;
        }
        try {
            List<String> areaCodes = Arrays.asList(jobParam.split("&"));
            List<CompletableFuture<Void>> futures = areaCodes.stream().map(areaCode -> {
                return CompletableFuture.runAsync(() -> {
                    try {
                        boolean result = domesticWeatherService.addBaiduWeather(areaCode);
                        if (result) {
                            CommonUtil.printThreadLog("百度天气数据添加成功，区域代码: " + areaCode);
                        } else {
                            CommonUtil.printThreadLog("百度天气数据添加失败，区域代码: " + areaCode);
                        }
                    } catch (Exception ex) {
                        CommonUtil.printThreadLog("百度天气数据添加异常，区域代码: " + areaCode + ", 异常信息: " + ex.getMessage());
                    }
                });
            }).collect(Collectors.toList());

            // 合并所有future并等待它们全部完成
            CompletableFuture<Void> allFutures = CompletableFuture.allOf(
                    futures.toArray(new CompletableFuture[0])).thenRun(() -> {
                        CommonUtil.printThreadLog("所有百度天气数据处理完成");
                    });

            // 等待所有异步全部执行完毕
            allFutures.join();

            // for (String areaCode : areaCodes) {
            // try {
            // boolean result = domesticWeatherService.addBaiduWeather(areaCode);
            // if (result) {
            // CommonUtil.printThreadLog("百度天气数据添加成功，区域代码: " + areaCode);
            // } else {
            // CommonUtil.printThreadLog("百度天气数据添加失败，区域代码: " + areaCode);
            // }
            // } catch (Exception ex) {
            // CommonUtil.printThreadLog("百度天气数据添加异常，区域代码: " + areaCode + ", 异常信息: " +
            // ex.getMessage());
            // }
            // }
        } catch (Exception ex) {
            CommonUtil.printThreadLog("百度天气数据添加异常，Job参数: " + jobParam + ", 异常信息: " + ex.getMessage());
        }
    }
}
